<template>
  <el-dialog
    title="鞋子配饰拍照"
    :visible="dialogVisible"
    width="95vw"
    center
    append-to-body
    style="background-color: rgba(0, 0, 0, 0.6); z-index: 2013"
    :before-close="handleClose"
  >
    <div style="display: flex">
      <div style="flex: 0 0 40vw">
        <div style="margin-bottom: 30px">
          <el-button type="primary" @click="photography"> 拍照 </el-button>
          <el-button type="primary" @click="handleComplete">
            拍摄完成
          </el-button>
        </div>
        <div class="image-box">
          <div class="image-item" v-for="(item, index) in pictureList">
            <i class="el-icon-error" @click="deleteImage(index)" />
            <el-image
              style="width: 50px; height: 50px; margin-right: 5px"
              :src="item.url"
              :preview-src-list="pictureList.map((item) => item.url)"
            >
            </el-image>
          </div>
        </div>
      </div>

      <div style="flex: 1">
        <CameraCaptureTask
          v-if="cameraCaptureVisible"
          ref="cameraCaptureTask"
          @success="success"
          @close="cameraCaptureVisible = false"
        />
      </div>
    </div>
  </el-dialog>
</template>

<script>
import CameraCaptureTask from "@/components/CameraCaptureTask/CameraCaptureTask.vue";
import { v4 as uuidv4 } from "uuid";
import { base64DataToFile } from "@/utils/file";
import { getOssClient } from "../../../../utils/tool";
import dayjs from "dayjs";
export default {
  props: ["accVisible", "photos"],
  components: {
    CameraCaptureTask,
  },
  data() {
    return {
      dialogVisible: false,
      pictureList: [],
      cameraCaptureVisible: false,
    };
  },
  watch: {
    accVisible(val) {
      this.$nextTick(() => {
        this.dialogVisible = val;
        this.pictureList = JSON.parse(JSON.stringify(this.photos));
      });
    },
    dialogVisible(val) {
      this.cameraCaptureVisible = val;
    },
  },
  created() {
    document.addEventListener("keydown", this.physicalPhotography);
  },
  destroyed() {
    document.removeEventListener("keydown", this.physicalPhotography);
  },
  methods: {
    photography() {
      this.$refs.cameraCaptureTask.photograph();
    },
    physicalPhotography(event) {
      if (event.key === "F3") {
        event.preventDefault();
        event.stopPropagation();
        this.$refs.cameraCaptureTask.photograph();
      }
    },

    handleClose() {
      this.$emit("close");
    },
    success(photos) {
      const uploadList = [];
      photos.forEach((item) => {
        const uuid = uuidv4();
        this.pictureList.push({
          url: item,
          data: null,
          status: "upload",
          uuid,
        });

        uploadList.push({
          url: item,
          uuid,
        });
      });

      this.uploadImage(uploadList);
    },

    async uploadImage(waitUploadImageList) {
      try {
        const client = await getOssClient();

        // 等待上传的图片
        const requestList = waitUploadImageList.map((item) => {
          const fileName = `public/img/${dayjs().format(
            "YYYY-MM-DD"
          )}/${uuidv4()}.png`;
          item.efficient = fileName;
          return client.put(fileName, base64DataToFile(item.url));
        });

        const resultList = await Promise.all(requestList);

        resultList.forEach((res, index) => {
          const imageObject = waitUploadImageList[index];
          // 对应索引
          const shoePitureIndex = this.pictureList.findIndex(
            (item) => item.uuid === imageObject.uuid
          );
          if (shoePitureIndex === -1) {
            return;
          }
          // 改变图片状态
          this.pictureList.splice(shoePitureIndex, 1, {
            url: this.pictureList[shoePitureIndex].url,
            data: waitUploadImageList[index].efficient,
            status: "success",
            uuid: uuidv4(),
          });
          this.pictureList = [...this.pictureList];
        });
      } catch (error) {
        console.error(error);
      }
    },

    handleComplete() {
      this.$emit("success", this.pictureList);
      this.dialogVisible = false;
      this.cameraCaptureVisible = false;
    },

    deleteImage(index) {
      this.pictureList.splice(index, 1);
    },
  },
};
</script>

<style lang="scss" scoped>
.image-box {
  width: 220px;
  display: flex;
  flex-wrap: wrap;
  .image-item {
    position: relative;
    flex: 0 0 50px;
    &:nth-child(4n) {
      margin-right: 0;
    }
    .el-icon-error {
      position: absolute;
      right: 0;
      top: 0;
      z-index: 1;
      background-color: #ffffff;
      border-radius: 50%;
      cursor: pointer;
    }
  }
}
</style>
